<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-11-06 13:07:07
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Http\Services\Market;

use App\Http\Services\Service;
use App\Repositories\CustomerComplaintRepository;
use App\Repositories\CustomerComplaintDetailsRepository;
use Illuminate\Support\Facades\DB;

/**
 * @property CustomerComplaintRepository $repository
 * @property CustomerComplaintDetailsRepository $details  
 */
class CustomerComplaintService extends Service
    {
    public $details;

    public function boot(
        CustomerComplaintRepository $repository,
        CustomerComplaintDetailsRepository $details,
    ) {
        $this->repository = $repository;
        $this->details    = $details;
        }

    public function details()
        {
        // DB::enableQueryLog();
        $data = $this->details->condition(request())->getGrid();
        // dd(DB::getQueryLog());
        return $data;
        }
    protected function save()
        {
        $data = request()->toArray();

        $purchase_data['id']                 = !isEmpty($data['id'] ?? "") && $data['id'] > 0 ? $data['id'] : 0;
        $purchase_data['billdate']           = $data['billdate'];
        $purchase_data['complaint_id']       = $data['complaint_id'];
        $purchase_data['relation_code']      = $data['relation_code'];
        $purchase_data['company_id']         = $data['company_id'];
        $purchase_data['department_id']      = $data['department_id'];
        $purchase_data['remark']             = $data['remark'];
        $purchase_data['attachment']         = $data['attachment'];
        $purchase_data['iscustomer']         = 1;
        $purchase_data['red_id']             = $data['red_id'];
        $purchase_data['material_id']        = $data['material_id'];
        $purchase_data['complaint_id']       = $data['complaint_id'];
        $purchase_data['amount']             = 0;
        $purchase_data['auxiliary_quantity'] = 0;
        $purchase_data['quantity']           = 0;
        $purchase_data['details']            = $data['details'];


        if ($purchase_data['id'] !== 0) {
            $gdata = $this->repository->whereId($purchase_data['id'])->getOne();
            if ($gdata->status_id != '0') {
                fail("订单状态[{$gdata->status->name}],禁止编辑");
                }
            }

        //退货
        if ($data['complaint_id'] == 2) {
            if (!is_array($data['details']) || count($data['details']) == 0)
                fail("退仓必须选择库存！");
            }
        $gdata = $this->repository->autosave($purchase_data);
        $gdata->details->filter(function ($v) use (&$gdata, $data)
            {
            $v->code                    = $gdata->code;
            $v->billdate                = $gdata->billdate;
            $v->department_id           = $data['department_id'];
            $v->lot_name                = $data['lot_name'];
            $v->amount                  = -$v->amount;
            $v->quantity                = -$v->quantity;
            $v->auxiliary_quantity      = -$v->auxiliary_quantity;
            $v->fect_quantity           = -$v->fect_quantity;
            $v->fect_auxiliary_quantity = -$v->fect_auxiliary_quantity;
            if ($v->quantity < 0) {
                $v->useStorage("客户退仓", $v->code, -$v->fect_quantity, -$v->fect_auxiliary_quantity);
                }
            $gdata->amount += $v->amount;
            $gdata->auxiliary_quantity += $v->auxiliary_quantity;
            $gdata->quantity += $v->quantity;

            });

        return $gdata->save();
        }


    protected function delete()
        {
        $ids = [];
        $this->details->whereIn("id", $this->getRequestIds())->get()->filter(function ($detail) use (&$ids)
            {
            if ($detail->status_id != 0)
                fail("单据已[{$detail->status->name}]！删除失败！");
            $ids[$detail['main_id']] = $detail['main_id'];
            //设置订单剩余可入库数量
            if ($detail->quantity < 0) {
                $detail->nouseStorage();
                }
            $detail->delete();
            });
        $newids   = $this->details->whereIn("main_id", $ids)->pluck("main_id");
        $newids[] = 0;
        $data     = $this->repository
            ->whereIn("id", $ids)
            ->whereNotIn("id", $newids)->get();

        $data->filter(function ($storage)
            {

            $storage->delete();
            });
        return $data;

        }

    }